﻿using System;
using System.Data;
using System.Windows.Forms;
using KTD_XSMS.Properties;
using KTDSMS;

namespace KTD_XSMS
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void userName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                passWord.Focus();
            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            string uname = (string)null;
            string upass = (string)null;
            string text = (string)null;
            if (string.IsNullOrEmpty(this.userName.Text.Trim()))
                text = "Please enter User Name";
            else
                uname = this.userName.Text.Trim();
            if (string.IsNullOrEmpty(text))
            {
                if (string.IsNullOrEmpty(this.passWord.Text.Trim()))
                    text = "Please enter Password";
                else
                    upass = new StringHash().Encrypt(this.passWord.Text.Trim());
            }
            if (string.IsNullOrEmpty(text))
            {
                if (this.CheckExistUser(uname, upass))
                {
                    Settings.Default.LoginUser = uname;
                    Settings.Default.LoginPass = upass;
                    Settings.Default.CheckLogin = "Yes";
                    Settings.Default.Save();
                    base.Dispose();
                }
                else
                {
                    int num1 = (int)MessageBox.Show("Invalid UserName / Password.");
                }
            }
            else
            {
                int num2 = (int)MessageBox.Show(text);
                userName.Focus();
            }

            
            
        }

        private bool CheckExistUser(string uname, string upass)
        {
            // Doc du lieu tu file XML ra datatable
            string filename = Application.StartupPath + "\\Login.xml";
            DataTable tblLogin = new DataTable();
            tblLogin = XMLManager.ReadToXML(filename);

            if (tblLogin.Rows.Count > 0)
            {
                for (int i = 0; i < tblLogin.Rows.Count; i++)
                {
                    if (uname == tblLogin.Rows[i]["User"].ToString() && upass == tblLogin.Rows[i]["Pass"].ToString())
                        return true;
                    else return false;
                }
            }
            return false;
        }
        private void passWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.buttonLogin_Click(sender,e);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            base.Dispose();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            Settings.Default.CheckLogin = "";
            Settings.Default.Save();
        }

        
    }
}
